package com.ljc.service;

import com.ljc.dao.DevicePurchasePlanningArgumentDao;
import com.ljc.dao.ProjectPlanningArgumentDao;
import com.ljc.dao.ProjectPlanningDao;
import com.ljc.entity.DevicePurchasePlanning;
import com.ljc.entity.DevicePurchasePlanningArgument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DevicePurchasePlanningArgumentServiceImpl implements DevicePurchasePlanningArgumentService {

    @Autowired
    private DevicePurchasePlanningArgumentDao devicePurchasePlanningArgumentDao;

    @Autowired
    private ProjectPlanningArgumentDao projectPlanningArgumentDao;

    @Autowired
    private ProjectPlanningDao projectPlanningDao;

    /**
     *
     * */
    @Override
    public List<DevicePurchasePlanning> findByExpertAndProject(int projectPlanningId, int expertId) {
        return devicePurchasePlanningArgumentDao.findByExpertAndProject(projectPlanningId, expertId);
    }

    @Override
//    synchronized
    public void argument(DevicePurchasePlanningArgument argument, int expertId) {

        devicePurchasePlanningArgumentDao.argument(argument);
        List<DevicePurchasePlanning> devicePurchasePlannings = devicePurchasePlanningArgumentDao.findByExpertAndProject(argument.getProjectPlanningId(), expertId);
        boolean flag = true;
        //该专家对该项目所有设备论证是否已经完成
        for (DevicePurchasePlanning d : devicePurchasePlannings) {
            if (d.getArgumentStatus() == 1)
                flag = false;
        }
        if (flag) { //更新专家项目论证状态
            projectPlanningArgumentDao.setStatus(argument.getProjectPlanningId(), expertId,2);
            boolean finish = true;
            int count = projectPlanningArgumentDao.countUnArgument(argument.getProjectPlanningId());
            if (count == 0) {
                //更新项目状态  8已完成论证 ---教务处汇总论证信息
                projectPlanningDao.setStatus(argument.getProjectPlanningId(),8);
            }
        }

    }

    @Override
    public DevicePurchasePlanningArgument getById(int id) {
        return devicePurchasePlanningArgumentDao.getById(id);

    }

    @Override
    public List<DevicePurchasePlanningArgument> getByDevicePurchasePlanningId(int devicePurchasePlanningId) {
        return devicePurchasePlanningArgumentDao.getByDevicePlanningId(devicePurchasePlanningId);
    }


}
